package com.education.serviceedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.education.commonutils.ResultAjax;
import com.education.servicebase.config.exceptionjandler.GuliException;
import com.education.serviceedu.entity.EduCourse;
import com.education.serviceedu.entity.EduTeacher;
import com.education.serviceedu.entity.vo.CourseInfoVo;
import com.education.serviceedu.entity.vo.CoursePublishVo;
import com.education.serviceedu.entity.vo.CourseQuery;
import com.education.serviceedu.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author zwr
 * @since 2021-05-23
 */
@RestController
@RequestMapping("/serviceedu/course")
@CrossOrigin
@Api(tags = "课程管理")
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    /**
     * 课程列表
     * 条件查询带分页
     * @return
     */
    @ApiOperation(value = "课程列表")
    @PostMapping("pageCourseList/{current}/{limit}")
    public ResultAjax resultAjax(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) CourseQuery courseQuery){
        //创建page对象
        Page<EduCourse> pageCourse = new Page<>(current, limit);
        //构建条件
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql
        String title=courseQuery.getTitle();
        String status=courseQuery.getStatus();
        //判断条件值是否为空，如果不为空拼接条件
        if (!StringUtils.isEmpty(title)) {
            //构建条件
            wrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }

        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        eduCourseService.page(pageCourse, wrapper);

        long total = pageCourse.getTotal();
        List<EduCourse> records = pageCourse.getRecords();
        return ResultAjax.success().data("total", total).data("rows", records);
    }

    /**
     * 添加课程基本信息的方法
     * @param courseInfoVo
     * @return
     */
    @ApiOperation("添加课程信息")
    @PostMapping("addCourseInfo")
    public ResultAjax addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        //返回添加之后课程id，为了后面添加大纲使用
        String id = eduCourseService.saveCourseInfo(courseInfoVo);
        return ResultAjax.success().data("courseId",id);
    }

    /**
     * 根据课程id查询课程基本信息
     * @param courseId
     * @return
     */
    @ApiOperation(value = "根据课程id查询课程信息")
    @GetMapping("getCourseInfo/{courseId}")
    public ResultAjax getCourseInfo(@ApiParam(name = "courseId",value = "课程id",required = true) @PathVariable String courseId){
        CourseInfoVo courseInfo = eduCourseService.getCourseInfo(courseId);
        return ResultAjax.success().data("courseInfoVo",courseInfo);
    }

    /**
     * 修改课程信息
     * @param courseInfoVo
     * @return
     */
    @ApiOperation(value = "修改课程信息")
    @PostMapping("updateCourseInfo")
    public ResultAjax updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        eduCourseService.updateCourseInfo(courseInfoVo);
        return ResultAjax.success();
    }

    /**
     * 根据课程id查询课程确认信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据课程id查询课程确认信息")
    @GetMapping("getPublishCourseInfo/{id}")
    public ResultAjax getPublishCourseInfo(@PathVariable String id) {
        CoursePublishVo coursePublishVo = eduCourseService.publishCourseInfo(id);
        return ResultAjax.success().data("publishCourse",coursePublishVo);
    }

    /**
     * 课程最终发布
     * 修改课程状态
     * @param id
     * @return
     */
    @ApiOperation(value = "课程最终发布,修改课程状态")
    @PostMapping("publishCourse/{id}")
    public ResultAjax publishCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        //设置课程发布状态
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return ResultAjax.success();
    }

    /**
     * 删除课程
     * @param courseId
     * @return
     */
    @ApiOperation(value = "删除课程")
    @DeleteMapping("{courseId}")
    public ResultAjax deleteCourse(@PathVariable String courseId) {
        eduCourseService.removeCourse(courseId);
        return ResultAjax.success();
    }
}

